﻿/**
* Class:  
* Version: alpha
* Author:  Pavel Jacko
* Date:   18/05/2009
* Description:
* 
*/
package syerit.core.ui
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/** 
	 * @author Pavel Jacko
	 */
	public class ImageButton extends Sprite
	{
		public var imageOut:Bitmap;
		public var imageOver:Bitmap;
		public var imageOutSource:DisplayObject;
		public var imageOverSource:DisplayObject;
		private var trigger:Function;
		private var triggerParams:Array;
		
		public var disableEventPropagation:Boolean = false;
		
		public function ImageButton(imageOutSource:DisplayObject,imageOverSource:DisplayObject,x:Number=0,y:Number=0,trigger:Function=null,triggerParams:Array=null,index:int=0)
		{
			super();
			
			this.x=x;
			this.y=y;
			this.imageOutSource=imageOutSource;
			this.imageOverSource=imageOverSource;
			this.trigger= trigger;
			this.triggerParams= triggerParams;
			
			register();
		}
		
		public function register():void
		{
			var bmdOut:BitmapData = new BitmapData(imageOutSource.width,imageOutSource.height,true,0x000000);
			bmdOut.draw(imageOutSource);
			imageOut = new Bitmap(bmdOut);
			//bmdOut.dispose();
			
			var bmdOver:BitmapData = new BitmapData(imageOverSource.width,imageOverSource.height,true,0x000000);
			bmdOver.draw(imageOverSource);
			imageOver = new Bitmap(bmdOver);
			//bmdOver.dispose();
			
			activate();
		}
		
		public function activate():void
		{
			this.buttonMode = true;
			addChild(imageOut);
			if(trigger!=null)
			{
				addEventListener(MouseEvent.CLICK,mouseClickHandler);
			}
			addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler);
		}
		
		public function deactivate():void
		{
			this.buttonMode = false;
			removeEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler);
			removeEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler);
			removeEventListener(MouseEvent.CLICK,mouseClickHandler);
		}
		
		public function dispose():void
		{
			deactivate();
		}
		
		private var _toggle:Boolean = false;
		
		public function set toggle(value:Boolean):void
		{
			_toggle = value;
			if(_toggle)
			{
				addEventListener(MouseEvent.CLICK,toggleClickHandler);
			}
			else
			{
				removeEventListener(MouseEvent.CLICK,toggleClickHandler);
			}
			selected = false;
		}
		
		public function get toggle():Boolean
		{
			return _toggle;
		}
		
		public var selected:Boolean = false;
		
		private function mouseOverHandler(e:MouseEvent):void
		{
			if(disableEventPropagation)
			{
				e.stopImmediatePropagation();
			}
			if(!selected)
			{
				removeChildAt(0);
				addChild(imageOver);
			}
		}
		
		private function mouseOutHandler(e:MouseEvent):void
		{
			if(disableEventPropagation)
			{
				e.stopImmediatePropagation();
			}
			if(!selected)
			{
				removeChildAt(0);
				addChild(imageOut);
			}
		}
		
		private function toggleClickHandler(e:MouseEvent):void
		{
			if(_toggle)
			{
				selected = !selected;
				if(!selected)
				{
					mouseOutHandler(null);
				}
			}
		}
		
		private function mouseClickHandler(e:MouseEvent):void
		{
			if(disableEventPropagation)
			{
				e.stopImmediatePropagation();
			}
			trigger.apply(this,triggerParams);
		}
		
		public function set smoothing(val:Boolean):void
		{
			imageOut.smoothing = val;
			imageOver.smoothing = val;
		}
		
	}
}